package com.yunduo.service.impl;

import com.yunduo.common.JwtUtils;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.ResultCode;
import com.yunduo.common.YunDuoConst.OrderState;
import com.yunduo.common.YunDuoUtils;
import com.yunduo.entity.Order;
import com.yunduo.entity.Pay;
import com.yunduo.mapper.PayMapper;
import com.yunduo.service.OrderService;
import com.yunduo.service.PayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ChengXin
 * @since 2023-08-25
 */
@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements PayService {

    @Resource
    private OrderService orderService;

    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean localPay(String orderId, String intable) {
        Order order = orderService.getById(orderId);
        if(!order.getState().equals(OrderState.DELAY_PAY)){
            return false;
        }
        order.setState(OrderState.DELAY_SEND);
//        order.setAddress(intable);
        orderService.updateById(order);
        String userId = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest()).getId();
        Pay pay = new Pay();
        pay.setOrderId(order.getId());
        pay.setPayType(OrderState.UNLINE);
        pay.setMoney(order.getPay().toString());
        pay.setIntable(intable);
        pay.setUserId(userId);
        this.save(pay);
        return true;
    }
}
